package java_thinking.unit_12;

/**
 * @Description : 重新抛出异常
 *      如果只是把当前异常对象重新抛出，那么printStackTrace()方法显示的将是原来
 *      异常抛出点的调用栈信息，并非重新抛出点的信息，想要更新这个信息，可以调用fillInStackTrace()方法，
 *      这将返回一个Throwable对象，他是通过把当前调用栈信息填入原来那个异常对象而建立的
 * @author: cww
 * @DateTime: 2019/3/21 8:49
 */

public class Rethrowing {
    public static void f() throws Exception {
        System.out.println("originating the exception in f()");
        throw new Exception("throw from f()");
    }
    public static void g() throws Exception {
        try {
            f();
        } catch (Exception e) {
            System.out.println("Inside g(),e.printStackTrace()");
//            e.printStackTrace(System.out);
            throw e;
        }
    }

    public static void h() throws Exception {
        try {
            f();
        } catch (Exception e) {
            System.out.println("Inside h(),e.printStackTrace()");
//            e.printStackTrace(System.out);
            throw (Exception)e.fillInStackTrace();
        }

    }

    public static void main(String[] args) {
        try {
            g();
        } catch (Exception e) {
            System.out.println("Inside h(),e.printStackTrace()");
            e.printStackTrace(System.out);
        }
//        try {
//            h();
//        } catch (Exception e) {
//            System.out.println("maindemo:printStackTrace");
//            e.printStackTrace(System.out);
//        }
    }
}
    /*originating the exception in f()
    Inside g(),e.printStackTrace()
        Inside h(),e.printStackTrace()
        java.lang.Exception: throw from f()
        at java_thinking.unit_12.Rethrowing.f(Rethrowing.java:15)
        at java_thinking.unit_12.Rethrowing.g(Rethrowing.java:19)
        at java_thinking.unit_12.Rethrowing.maindemo(Rethrowing.java:40)*/
/*
    originating the exception in f()
    Inside h(),e.printStackTrace()
        maindemo:printStackTrace
        java.lang.Exception: throw from f()
        at java_thinking.unit_12.Rethrowing.h(Rethrowing.java:33)
        at java_thinking.unit_12.Rethrowing.maindemo(Rethrowing.java:46)*/
